/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Edu
 */
@WebServlet(name = "FiltroServlet", urlPatterns = {"/FiltroServlet"})
public class FiltroServlet extends HttpServlet {
    
    //Creación de variables
    
    String cat = null;
    Connection conn = null;
    String bbddString = new String("jdbc:derby://localhost:1527/tienda_edomingo;create=true");
    
    //Conexión BBDD
    public void init(ServletConfig config) throws ServletException {
    super.init(config);
    try{
        Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
    }catch(ClassNotFoundException ex){
        System.out.println("Error al cargar el driver");
        System.out.println(ex.getMessage());
    }

    try{
        conn = DriverManager.getConnection(bbddString);
    }catch (SQLException sqlEx){
        System.out.println("Error al establecer la conexión ");
        System.out.println(sqlEx.getMessage());
    }
        System.out.println("Iniciando ServletOpinion (versión BD)..");
    }
    
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
               
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(FiltroServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            devolverPaginaHTML(request,response);
        } catch (SQLException ex) {
            Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void devolverPaginaHTML(HttpServletRequest req, HttpServletResponse resp) throws IOException, SQLException{
            
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        cat = req.getParameter("categoria");
        System.out.println(cat);
        Statement s = conn.createStatement();
        ResultSet rs = s.executeQuery ("select * from producto where categoria='"+cat+"'");       
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
            
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet login</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>"+cat+"</h1>");
            while (rs.next())
            {
                out.println (rs.getString(2));
                out.println("<br>");
                out.println("<input type='button' name='add' value='Añadir a carrito'/>");
                out.println("<br>");
            }
            out.println("</body>");
            out.println("</html>");    
        } finally {            
            out.close();
        }

        out.flush();
        out.close();
        //conn.close();
        } 
    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
